package com.bwie.mq;

import com.bwie.common.domain.GroupActivity;
import com.bwie.exception.ActivityException;
import com.bwie.mapper.ActivityMapper;
import com.bwie.stutas.Status;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @className: GroupBuyingMQ
 * @Description: TODO
 * @version: v1.８.0
 * @author: 刘茂锴
 * @date: 2025/10/3 10:13
 */
@Component
@Log4j2
public class GroupBuyingMQ {
    @Autowired
    private ActivityMapper activityMapper;
    @RabbitListener(queuesToDeclare = @Queue(name = "2302A"))
    public void groupBuyingMq(Integer activityId){
        //查询拼团活动
        GroupActivity groupActivity=activityMapper.selectActivity(activityId);
        long time = groupActivity.getEndTime().getTime();
        long time1 = new Date().getTime();
        if (time1>time){
            activityMapper.updStatus(activityId, Status.JIE);
            log.info("拼团活动已结束，活动名称为: {}", groupActivity.getTitle());
        }
    }

    @Scheduled(cron = "0 */5 * * * *")
    public void selectActivityStatus(){
        //查询所有正在拼团的
        List<GroupActivity> list=activityMapper.selectActivityStatus();
        list.forEach(activity->{
            long time = activity.getEndTime().getTime();
            long time1 = new Date().getTime();
            if (time1>time){
                activityMapper.updStatus(activity.getId(),Status.JIE);
                log.info("拼团活动已结束，活动名称为: {}", activity.getTitle());
            }
        });
    }
}
